package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeBillItems;
import com.kehutong.charge.entity.ChargeFeeConfig;
import com.kehutong.charge.service.ChargeBillItemsService;
import com.kehutong.charge.service.ChargeBillManageService;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: kht
 * @description: 单项收费项
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/bill/items")
public class ChargeBillItemsController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeBillItemsController.class);

    @Inject
    DeptService deptService;

    @Inject
    ChargeBillItemsService chargeBillItemsService;

    @ReqMapping("/page")
    @HasPermission("pay:charge:bill:items:view")
    public Object page(JSONObject jsonObject) throws Exception {
        return chargeBillItemsService.findPage(jsonObject);
    }


    /**
     * 类目账单列表导入检查
     * @param jsonObject
     */
    @ReqMapping("/import/check")
    public Object importExcelCheck(JSONObject jsonObject){
        chargeBillItemsService.importExcelCheck(jsonObject);
        return Result.success();
    }

    /**
     * 类目账单列表导入
     * @param jsonObject
     * @param token
     * @Author jian Ye
     */
    @ReqMapping("/import")
    public Object importExcel(JSONObject jsonObject, Token token) throws Exception {
        return chargeBillItemsService.importExcel(jsonObject,token);
    }


    /**
     * 类目收费账单导入模板生成
     * @param req
     * @param resp
     */
    @ReqMapping("/template/export")
    @HasPermission("pay:charge:meter:reading:view")
    public void templateExport(HttpServletRequest req, HttpServletResponse resp){
        chargeBillItemsService.templateExport(req,resp);
    }


    @ReqMapping("/page/export")
    @HasPermission("pay:charge:bill:items:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        chargeBillItemsService.pageExport(req,resp,jsonObject);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:bill:items:view")
    public Object list(JSONObject jsonObject) {
        return chargeBillItemsService.findList(jsonObject);
    }


    @ReqMapping("/listLadder")
    @HasPermission("pay:charge:bill:items:view")
    public Object listLadder(JSONObject jsonObject) {
        return chargeBillItemsService.findBillLadderList(jsonObject);
    }


    @ReqMapping("/get")
    public Object get(ChargeBillItems entity) {

        return Result.success(entity);
    }


    @ReqMapping("/getObj")
    public Object getObj(JSONObject jsonObject) {

        return Result.success(chargeBillItemsService.getObj(jsonObject));
    }


    @HasPermission("pay:charge:bill:items:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeFeeConfig entity) {

        return chargeBillItemsService.deleteById(entity.getId());
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:bill:items:edit")
    @ReqMapping("/save")
    public Object saveHouse(ChargeBillItems entity, Token token) throws Exception {
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());
            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setCreateName(token.getUsername());
        entity.setRemark(token.getCompanyNo());
        return chargeBillItemsService.save(entity);
    }


    /**
     * 修改方法
     * @param entity
     * @return
     */
    @ReqMapping("/update")
    @HasPermission("pay:charge:bill:items:edit")
    public Object update(ChargeBillItems entity, Token token) throws Exception {

        entity.setUpdateName(token.getUsername());
        return chargeBillItemsService.update(entity);
    }


    /**
     * 作废类目
     * @param jsonObject
     * @return
     */
    @ReqMapping("/cancelItemBill")
    @HasPermission("pay:charge:bill:items:edit")
    public Object cancelItemBill(JSONObject jsonObject, Token token) throws Exception {

        return chargeBillItemsService.cancelItemBill(jsonObject,token);
    }

}
